package com.leo.blog.web.visit;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leo.blog.annotatiton.AccessLimit;
import com.leo.blog.dto.VisitExecution;
import com.leo.blog.entity.Visit;
import com.leo.blog.enums.VisitStateEnum;
import com.leo.blog.service.VisitService;
import com.leo.blog.util.HttpServletRequestUtil;

@Controller
@RequestMapping(value = "/visit")
public class VisitManagementController {
	@Autowired
	private VisitService visitService;
	
	@RequestMapping(value = "/updatevisit")
	@ResponseBody
	public Map<String, Object> updateVisit(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		int visitId = HttpServletRequestUtil.getInt(request, "visitId");
		String visitIp = HttpServletRequestUtil.getString(request, "visitIp");
		String userAgent = HttpServletRequestUtil.getString(request, "userAgent");
		String city = HttpServletRequestUtil.getString(request, "city");
		String url = HttpServletRequestUtil.getString(request, "url");
		String platformType = HttpServletRequestUtil.getString(request, "platformType");
		String browserType = HttpServletRequestUtil.getString(request, "browserType");
		
		Visit visit = new Visit();
		if(visitId > 0) {
			visit.setVisitId(visitId);
			
			if(visitIp != null && visitIp != "") {
				visit.setVisitIp(visitIp);
			}
			if(userAgent != null && userAgent != "") {
				visit.setUserAgent(userAgent);
			}
			if(city != null && city != "") {
				visit.setCity(city);
			}
			if(url != null && url != "") {
				visit.setUrl(url);
			}
			if(platformType != null && platformType != "") {
				visit.setPlatformType(platformType);
			}
			if(browserType != null && browserType != "") {
				visit.setBrowserType(browserType);
			}
			
			VisitExecution ve = visitService.updateVisit(visit);
			if(ve.getState() == VisitStateEnum.SUCCESS.getState()) {
				modelMap.put("success", true);
			}else {
				modelMap.put("success", false);
				modelMap.put("msg", "更新失败");
			}
			
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "id错误");
		}
		
		return modelMap;
	}
	
	@RequestMapping(value = "/getvisitlistbyip")
	@ResponseBody
	public Map<String, Object> getVisitListByIp(){
		Map<String, Object> modelMap = new HashMap<>();
		VisitExecution ve = visitService.getVisitListByIp();
		if(ve.getState() == VisitStateEnum.SUCCESS.getState()) {
			modelMap.put("list", ve.getVisitList2());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据失败");
		}
		
		return modelMap;
	} 
	
	/**
	 * 模糊组合分页查询访客信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getvisitlistbygroupip")
	@ResponseBody
	public Map<String, Object> getVisitListByGroupIp(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
				
		//获取前台传过来的值
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String visitIp = HttpServletRequestUtil.getString(request, "visitIp");
		String city = HttpServletRequestUtil.getString(request, "city");
		String platformType = HttpServletRequestUtil.getString(request, "platformType");
		String browserType = HttpServletRequestUtil.getString(request, "browserType");
		
		Visit visitCondition = new Visit();
		
		//判断
		if(startTime != null && startTime != "") {
			visitCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			visitCondition.setEndTime(endTime);
		}
		if(visitIp != null && visitIp != "") {
			visitCondition.setVisitIp(visitIp);
		}
		if(city != null && city != "") {
			visitCondition.setCity(city);
		}
		if(platformType != null && platformType != "") {
			visitCondition.setPlatformType(platformType);
		}
		if(browserType != null && browserType != "") {
			visitCondition.setBrowserType(browserType);
		}
		
		VisitExecution ve = visitService.getVisitListByGroupIp(visitCondition, pageIndex, pageSize);
		
		//返回页数
		int page = 0;
		int pageCount = ve.getCount();
		if(pageCount > pageSize) {
			if(pageCount%pageSize > 0) {
				page = pageCount/pageSize + 1;
			}else {
				page = pageCount/pageSize;
			}
			
		}else {
			page = 1;
		}
		
		if(ve.getState() == VisitStateEnum.SUCCESS.getState()) {
			modelMap.put("visitList", ve.getVisitList2());
			modelMap.put("count", ve.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "分页查询失败");
		}
		
		return modelMap;
	}
	
	/**
	 * 模糊组合分页查询访客信息
	 */
	@RequestMapping(value = "/getvisitlistbypage")
	@ResponseBody
	public Map<String, Object> getVisitListByPage(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的页码
		int pageIndex = HttpServletRequestUtil.getInt(request, "pageIndex");
		//获取前台传过来的每页要求返回的文章数上限
		int pageSize = HttpServletRequestUtil.getInt(request, "pageSize");
		
		//获取前台传过来的值
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String visitIp = HttpServletRequestUtil.getString(request, "visitIp");
		String city = HttpServletRequestUtil.getString(request, "city");
		String platformType = HttpServletRequestUtil.getString(request, "platformType");
		String browserType = HttpServletRequestUtil.getString(request, "browserType");
		
		Visit visitCondition = new Visit();
		
		//判断
		if(startTime != null && startTime != "") {
			visitCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			visitCondition.setEndTime(endTime);
		}
		if(visitIp != null && visitIp != "") {
			visitCondition.setVisitIp(visitIp);
		}
		if(city != null && city != "") {
			visitCondition.setCity(city);
		}
		if(platformType != null && platformType != "") {
			visitCondition.setPlatformType(platformType);
		}
		if(browserType != null && browserType != "") {
			visitCondition.setBrowserType(browserType);
		}
		
		VisitExecution ve = visitService.getVisitListByPage(visitCondition, pageIndex, pageSize);
		
		//返回页数
		int page = 0;
		int pageCount = ve.getCount();
		if(pageCount > pageSize) {
			if(pageCount%pageSize > 0) {
				page = pageCount/pageSize + 1;
			}else {
				page = pageCount/pageSize;
			}
			
		}else {
			page = 1;
		}
		
		if(ve.getState() == VisitStateEnum.SUCCESS.getState()) {
			modelMap.put("visitList", ve.getVisitList());
			modelMap.put("count", ve.getCount());
			modelMap.put("page", page);
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "分页查询失败");
		}
		
		return modelMap;
	}
	
	/**
	 * 按照不同时间查询访客的数量
	 */
	@RequestMapping(value = "/getvisitlistbydate")
	@ResponseBody
	@AccessLimit(seconds = 1, maxCount =  10)
	public Map<String, Object> getVisitListByDate(HttpServletRequest request){
		Map<String, Object> modelMap = new HashMap<>();
		//获取前台传过来的值
		String startTime = HttpServletRequestUtil.getString(request, "startTime");
		String endTime = HttpServletRequestUtil.getString(request, "endTime");
		String format = HttpServletRequestUtil.getString(request, "format");
		
		Visit visitCondition = new Visit();
		if(startTime != null && startTime != "") {
			visitCondition.setStartTime(startTime);
		}
		if(endTime != null && endTime != "") {
			visitCondition.setEndTime(endTime);
		}
		if(format != null && format != "") {
			visitCondition.setFormat(format);
		}
		
		VisitExecution ve = visitService.getVisitListByDate(visitCondition);
		if(ve.getState() == VisitStateEnum.SUCCESS.getState()) {
			modelMap.put("list", ve.getVisitList2());
			modelMap.put("success", true);
		}else {
			modelMap.put("success", false);
			modelMap.put("msg", "查询数据失败");
		}	
		return modelMap;
	}
}
